//
//  DHCategoryAndScenarioViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2017/12/25.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHCategoryAndScenarioViewController.h"
#import "DHCategoryModel.h"
#import "DHScenarioModel.h"
#import "DHCategoryCollectionViewCell.h"
#import "TagView.h"

@interface DHCategoryAndScenarioViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, TagViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *categoryButton;
@property (assign, nonatomic) HomeInterestSetting interestSetting;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLineLayout;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) UIView *scenarioContentView;
@property (strong , nonatomic) TagView * scenarioItemsView;

@property (strong, nonatomic) NSMutableArray *categoryArr;
@property (strong, nonatomic) NSMutableArray *scenarioArr;

@property (strong, nonatomic) DHCategoryModel *selectedCategoryModel;
@property (strong, nonatomic) DHScenarioModel *selectedScenarioModel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSafeAreaLayout;

@end

@implementation DHCategoryAndScenarioViewController

- (instancetype)initWithInterestSetting:(HomeInterestSetting)interestSetting
{
    self = [super init];
    if (self) {
        self.interestSetting = interestSetting;
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.interestSetting = HomeInterestSettingCategory;
    }
    return self;
}

- (NSMutableArray *)categoryArr{
    if (!_categoryArr) {
        _categoryArr = [NSMutableArray array];
    }
    return _categoryArr;
}

- (NSMutableArray *)scenarioArr{
    if (!_scenarioArr) {
        _scenarioArr = [NSMutableArray array];
    }
    return _scenarioArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([UIDevice systemVersion] < 11) {
        self.topSafeAreaLayout.constant = 64;
    }
    
    [self initView];
    NSLog(@"%@",[Login curLoginUser].sex);
    if ([[GlobalManager shareGlobalManager].user.sex isEqualToString:@"男"]) {
        [self.categoryButton setTitle:@"男装" forState:UIControlStateNormal];
    }else{
        [self.categoryButton setTitle:@"女装" forState:UIControlStateNormal];
    }
    
    [self loadData];
    if (self.interestSetting == HomeInterestSettingCategory) {
        [self categoryAction:nil];
    }else if (self.interestSetting == HomeInterestSettingScenario){
        [self scenarioAction:nil];
    }
}

- (void)initView{
    self.title = @"选择";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [backButton setImage:[UIImage imageNamed:@"home_close"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    WeakSelf(weakSelf)
    [backButton bk_addEventHandler:^(id sender) {
        [weakSelf dismissViewControllerAnimated:YES completion:^{
            
        }];
    } forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [doneButton setTitle:@"确定" forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [doneButton setTitleColor:[UIColor colorWithHexString:@"#4a4a4a"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
    [doneButton bk_addEventHandler:^(id sender) {
        [UserSelectedManager shareUserSelectedManager].category = self.selectedCategoryModel;
        [UserSelectedManager shareUserSelectedManager].scenario = self.selectedScenarioModel;
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHCategoryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kDHCategoryCollectionViewCell];
    
    if (!self.scenarioContentView) {
        self.scenarioContentView = [[UIView alloc] initWithFrame:self.contentView.bounds];
        self.scenarioContentView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.scenarioContentView];
        
        self.scenarioItemsView = [[TagView alloc] initWithFrame:CGRectMake(0, 0, self.scenarioContentView.width, 0)];
        self.scenarioItemsView.backgroundColor = [UIColor whiteColor];
        self.scenarioItemsView.delegate = self;
        [self.scenarioContentView addSubview:self.scenarioItemsView];
        
        self.scenarioContentView.hidden = YES;
    }
}

// 品类
- (IBAction)categoryAction:(id)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.topLineLayout.constant = 0;
        self.scenarioContentView.hidden = YES;
        self.collectionView.hidden = NO;
    } completion:^(BOOL finished) {
        
    }];
}

// 场景
- (IBAction)scenarioAction:(id)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.topLineLayout.constant = 97;
        self.scenarioContentView.hidden = NO;
        self.collectionView.hidden = YES;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)loadData{
    [self loadCategoryData];
    [self loadScenarioData];
}

- (void)loadCategoryData{
    [[DHNetAPIClient shareDHNetAPIClient] request_categoriesWithParams:@{@"sex":[GlobalManager shareGlobalManager].user.sex} andBlock:^(id data, NSError *error) {
        DHCategoryListModel *categoryList = [DHCategoryListModel modelWithDictionary:data];
        [self.categoryArr removeAllObjects];
        [self.categoryArr addObjectsFromArray:categoryList.categories];
        [self.collectionView reloadData];
    }];
}

- (void)loadScenarioData{
    [[DHNetAPIClient shareDHNetAPIClient] request_scenesAndBlock:^(id data, NSError *error) {
        DHScenarioListModel *scenarioList = [DHScenarioListModel modelWithDictionary:data];
        [scenarioList updateScenarioTitleArr];
        self.scenarioItemsView.arr = scenarioList.scenarioTitleArr;
        [self.scenarioArr removeAllObjects];
        [self.scenarioArr addObjectsFromArray:scenarioList.scenes];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.categoryArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHCategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHCategoryCollectionViewCell forIndexPath:indexPath];
    DHCategoryModel *categoryModel = [self.categoryArr objectAtIndex:indexPath.row];
    if (categoryModel._id == self.selectedCategoryModel._id) {
        categoryModel.isSelected = YES;
    }else{
        categoryModel.isSelected = NO;
    }
    [cell uploadCellWith:categoryModel];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DHCategoryModel *categoryModel = [self.categoryArr objectAtIndex:indexPath.row];
    self.selectedCategoryModel = categoryModel;
    [self.collectionView reloadData];
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return (CGSize){(kScreenWidth-10)/2, 64};
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark - TagViewDelegate
-(void)handleSelectTag:(NSString *)keyWord{
    [self.scenarioArr enumerateObjectsUsingBlock:^(DHScenarioModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([keyWord isEqualToString:obj.title]) {
            self.selectedScenarioModel = obj;
            *stop = YES;
        }
    }];
}

@end
